class Solution {
    public String removeKdigits(String num, int k) {
        Deque<Character> deque=new LinkedList<>();
        int len=num.length();
        for(int i=0;i<len;i++)
        {
            char c=num.charAt(i);
            while(!deque.isEmpty()&&k>0&&deque.peekLast()>c)
            {
                deque.pollLast();
                k--;
            }
            deque.offerLast(c);
        }
        for(int i=0;i<k;i++)
        {
            deque.pollLast();
        }
        StringBuilder buil=new StringBuilder();
        boolean bb=true;
        while(!deque.isEmpty())
        {
            char z=deque.pollFirst();
            if(bb &&z=='0')
            {
                continue;
            }
            bb=false;
            buil.append(z);
        }
        if(buil.length()==0)
        return "0";
        return buil.toString();
    }
}
